#include "Viewport.h"

//////////////////////////////////////////////////////////////////////////
Viewport::Viewport(void) : mDirty(true), mWidth(0), mHeight(0), mFOV(0), mZNear(0), mZFar(0)
{
	mProjectionMatrix.SetIdentity();
}


//////////////////////////////////////////////////////////////////////////
Viewport::~Viewport(void)
{
}

//////////////////////////////////////////////////////////////////////////
void Viewport::Init()
{
	ComputeProjectionMat();
}

//////////////////////////////////////////////////////////////////////////
void Viewport::InitProjectionMat(float FOV, float aWidth, float aHeight, float zNear, float zFar)
{
	mFOV = FOV;
	mWidth = aWidth;
	mHeight = aHeight;
	mZFar = zFar;
	mZNear = zNear;
	ComputeProjectionMat();
}

//////////////////////////////////////////////////////////////////////////
void Viewport::ComputeProjectionMat()
{
	//*
	
	const float tAspectRatio = mWidth / mHeight;
	const float zRange       = mZNear - mZFar;
	const float tanHalfFOV   = Math::tanf(Math::DegreeToRadian(mFOV / 2.0f));
	
	mProjectionMatrix.mMat[0][0] = 1.0f/(tanHalfFOV * tAspectRatio);	mProjectionMatrix.mMat[0][1] = 0.0f;				mProjectionMatrix.mMat[0][2] = 0.0f;						mProjectionMatrix.mMat[0][3] = 0.0 ;
	mProjectionMatrix.mMat[1][0] = 0.0f;								mProjectionMatrix.mMat[1][1] = 1.0f/tanHalfFOV;		mProjectionMatrix.mMat[1][2] = 0.0f;						mProjectionMatrix.mMat[1][3] = 0.0;
	mProjectionMatrix.mMat[2][0] = 0.0f;								mProjectionMatrix.mMat[2][1] = 0.0f;				mProjectionMatrix.mMat[2][2] = (-mZNear -mZFar)/zRange ;	mProjectionMatrix.mMat[2][3] = 2.0f * mZFar*mZNear/zRange;
	mProjectionMatrix.mMat[3][0] = 0.0f;								mProjectionMatrix.mMat[3][1] = 0.0f;				mProjectionMatrix.mMat[3][2] = 1.0f;						mProjectionMatrix.mMat[3][3] = 0.0;

	mDirty = false;
	//*/
}